package scc.project.web.controller.manage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import scc.project.entity.Supplier;
import scc.project.entity.vo.JsonVo;
import scc.project.entity.vo.PageVo;
import scc.project.entity.vo.RestFulPageVo;
import scc.project.service.SupplierService;


/**
 * 供应商管理的控制器
 *
 * @author 陈圳佳
 *         日期：2015/6/22
 */
@Controller
@RequestMapping("/manage/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    /**
     * 添加编辑客户信息
     */
    @RequestMapping(value = "/edit.html", method = RequestMethod.GET)
    public String edit(ModelMap modelMap) {
        modelMap.addAttribute("action", "/manage/supplier");
        return "/manage/supplier/edit";
    }

    /**
     * 修改客户信息
     */
    @RequestMapping(value = "/{id}.html", method = RequestMethod.GET)
    public String edit(@PathVariable("id") Long id, ModelMap modelMap) {
        Supplier supplier = supplierService.get(id);
        System.out.print("《》《》《》《。我的id" + id);
        modelMap.addAttribute("supplier", supplier);
        modelMap.addAttribute("action", "/manage/supplier");
        System.out.print("《》《》《》《。那是我的id哼" + id);
        return "/manage/supplier/edit";
    }


    /**
     * *
     * 搜索客户支持翻页
     *
     * @param page          页码
     * @param supplierNames 供应商
     * @param address       客户地址
     */
    @RequestMapping(value = "/list/{page}.html", method = RequestMethod.GET)
    public String list(@PathVariable("page") Integer page,
                       @RequestParam(value = "name", required = false) String supplierNames,
                       @RequestParam(value = "address", required = false) String address,
                       ModelMap modelMap) {
        System.out.print("Find....." + supplierNames);
        PageVo<Supplier> pageVo;
        try {
            pageVo = RestFulPageVo.convert(supplierService.find(page, address, supplierNames), ".html");
            pageVo.addProperty("data-pjax", "#content");
            modelMap.addAttribute("pageVo", pageVo);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return "/manage/supplier/list";
    }


    /**
     * 添加供货商，只支持ajax 的POST 请求
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JsonVo add(@RequestBody Supplier supplier) {
        JsonVo jsonVo = new JsonVo();
        try {
            boolean b = supplierService.save(supplier);
            System.out.print("<><><><><><><><>状态" + supplier.getDelStatus() + "<><><.我知道了~");
            jsonVo.setStatus(b).setMsg(b ? "添加成功" : "添加失败原因未知?");
        } catch (Exception e) {
            e.printStackTrace();
            jsonVo.setMsg(e.getMessage()).setStatus(false);
        }
        return jsonVo;
    }


    /**
     * 删除
     *
     * @param id 主键ID
     * @return JsonVo对象
     */
    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JsonVo delete(@PathVariable("id") Long id) {
        JsonVo jsonVo = new JsonVo();
        try {
            boolean b = supplierService.deleteById(id);

            jsonVo.setStatus(b).setMsg(b ? "删除成功" : "删除失败原因未知");
        } catch (Exception e) {
            e.printStackTrace();
            jsonVo.setStatus(false).setMsg(e.getMessage());
        }
        return jsonVo;
    }


    /**
     * 修改的请求，返回JSON，接收的也是JSON
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT)
    public JsonVo update(@RequestBody Supplier supplier) {
        JsonVo jsonVo = new JsonVo();
        try {
            boolean b = supplierService.updateById(supplier);
            jsonVo.setStatus(b).setMsg(b ? "修改成功" : "修改失败原因未知？");
        } catch (Exception e) {
            e.printStackTrace();
            jsonVo.setMsg(e.getMessage()).setStatus(false);
        }
        return jsonVo;
    }
}
